package com.itheima.common;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


//    @Autowired
//    private HttpSession session;


    //执行添加时候进行填充
    @Override
    public void insertFill(MetaObject metaObject) {

        log.info("start insert fill ....");

        //系统时间
        this.strictInsertFill(metaObject,
                "createTime",
                LocalDateTime.class,
                LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        //更新时间
        this.strictInsertFill(metaObject,
                "updateTime",
                LocalDateTime.class,
                LocalDateTime.now()); // 起始版本 3.3.0(推荐)

//        Long id = (Long) session.getAttribute("employee");
        //在线程中使用
        Long currentId = BaseContext.getCurrentId();
        //创建人
        this.strictInsertFill(metaObject,
                "createUser",
                Long.class,
                currentId);
        //修改人
        this.strictInsertFill(metaObject,
                "updateUser",
                Long.class,
                currentId); // 起始版本 3.3.0(推荐)


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");

        this.strictUpdateFill(metaObject,
                "updateTime",
                LocalDateTime.class,
                LocalDateTime.now()); // 起始版本 3.3.0(推荐)


//        Long id = (Long) session.getAttribute("employee");
        Long currentId = BaseContext.getCurrentId();

        //修改人
        this.strictUpdateFill(metaObject,
                "updateUser",
                Long.class,
                currentId); // 起始版本 3.3.0(推荐)

    }
}
